/*
 * Copyright(C) 2010 the original author or authors.
 *
 * Change History Log
 * ----------------------------------------------------
 * Version: 1.0
 * Date: 2011-5-23
 * Author: wangzl
 * ----------------------------------------------------
 */
package com.seemsea.web.admin.coupon;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.seemsea.entity.admin.coupon.Product;
import com.seemsea.service.admin.ProductService;

/**
 * 
 * 
 * @author wangzl
 * @since 2011-5-23
 */
@Controller
public class ProductController {
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value="/admin/coupon/productManage.nhtml")
    public String index(Model model,HttpServletRequest request) {
		List<Product> products = productService.getAll();
		model.addAttribute("products", products);
        return "/admin/coupon/productManage";
    }
	
	@RequestMapping(value="/admin/coupon/productAdd.nhtml")
    public String add(CouponForm couponForm,Model model,HttpServletRequest request) {
		String productCode = couponForm.getProduct().getCode();
		if(productCode!=null && !"".equals(productCode)){
			Product product = productService.get(productCode);
			couponForm.setProduct(product);
			couponForm.setAction("update");
		}
        return "/admin/coupon/productAdd";
    }
	
	@RequestMapping(value="/admin/coupon/productSave.nhtml")
    public String save(CouponForm couponForm,Model model,HttpServletRequest request) {
		Product product = couponForm.getProduct();
		String action = couponForm.getAction();
		if(action.equals("update")){
			couponForm.getProduct().setUpdateTime(new Date());
		}else{
			couponForm.getProduct().setCreateTime(new Date());
			couponForm.getProduct().setUpdateTime(new Date());
		}
		productService.operateProduct(product);
        return "redirect:/admin/coupon/productAdd.nhtml";
    }	
}
